package com.eibus.applicationconnector.dotnet;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;

import junit.framework.TestCase;

import com.eibus.util.system.EIBProperties;

/**
 * 
 * @author bjoseph Dec 10, 2007
 * 
 * This is a prerequisite test case. It tests if Dotnet 2 framework is installed
 * in the system and if the environment variables are present or not.
 */
public class Dotnet2FrameworkTest_Prerequisites extends TestCase {

    public void testPlatform() {
        System.out.println("OS: " + System.getProperty("os.name"));
        assertTrue(
                "Platform is not Windows. These test cases run on Windows only",
                System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0);
    }

    /* Not needed
    public void testEnv() {
        String errMsg = "DOTNET_SDK_HOME environment variable is not set. Refer https://wiki.cordys.com/display/cwiki/Building+Dotnet+component";
        assertNotNull(errMsg, System.getenv("DOTNET_SDK_HOME"));
        assertNotSame(errMsg, "", System.getenv("DOTNET_SDK_HOME"));
    }*/

    public void testLibraries() {
        String cordysLibFolder = EIBProperties.getInstallDir() + File.separator
                + "lib" + File.separator;
        assertFileExists(cordysLibFolder + "jnet.dll");
        assertFileExists(cordysLibFolder + "xmlForDotnet.dll");
    }

    private void assertFileExists(String file) {
        assertTrue(file + " does not exist.", new File(file).exists());
    }

    public void testGAC() {
        String dotnetHome = System.getenv("DOTNET_SDK_HOME");
        String gac = null;

        if (dotnetHome != null) {
            gac = "\"" + dotnetHome + "\\v2.0\\Bin\\gacutil" + "\"";
        } else {
            gac = "\""
                    + "C:\\Program Files\\Microsoft.NET\\SDK\\v2.0\\Bin\\gacutil.exe"
                    + "\"";
            if (!new File(gac).exists()) {
                gac = "gacutil";
            }
        }

        String[] command = { gac };
        assertTrue("Could not find " + gac, execute(command));
    }

    private boolean execute(String[] commandArray) {
        try {
            Process process = Runtime.getRuntime().exec(commandArray);
            new Dotnet2FrameworkTest_Prerequisites.ConsoleReader(process.getInputStream());
            new Dotnet2FrameworkTest_Prerequisites.ConsoleReader(process.getErrorStream());
            process.waitFor();
            process.destroy();
            return true;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return false;
        }
    }
    
    class ConsoleReader extends Thread
    {
        BufferedReader m_streamReader;
        
        ConsoleReader(InputStream is)
        {  
            m_streamReader = new BufferedReader(new java.io.InputStreamReader(is));
            this.setDaemon(true);
            this.start();
        }
        
        public void run()
        {  
            try
            {
                while(true)
                {  
                    String line = m_streamReader.readLine();
                    if(line == null)
                    {
                        break;
                    }
                    
                    System.out.println(line);
                }
                    
                this.m_streamReader.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }        
        }
    }

    public static void main(String[] a) {
        junit.textui.TestRunner.run(Dotnet2FrameworkTest_Prerequisites.class);
    }
}